<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_store.php');

class ControlStore{

	function __construct()
	{
		$this->ModuleStore = new ModuleStore();
	}

	/**************************************
	* 查询所有未删除的Store信息
	* @access  public
	*
	* @return  array
	**************************************/
	function StoreList()
	{
		$sql="SELECT `store_cat_id`,`store_cat_name`,`store_cat_remark`,`store_is_recommend`,`store_delete`,`operate_time`,`operater` FROM store_cat WHERE `store_delete`=0";
		$result = $this->ModuleStore->StoreList($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 查询所有未删除的产品信息
	* @access  public
	*
	* @return  array
	**************************************/
	function SProductlist()
	{
		$sql="SELECT won.id,won.cat_id,won.won_product_id,won.won_product_add_time,won.won_product_add_user,
		won.won_product_click,won.won_product_update_time,won.won_product_delete,won.won_product_state,
		pro.product_name,pro.product_version,pro.product_price,pro.product_sn FROM won_product AS won
		LEFT JOIN product AS pro ON won.won_product_id=pro.product_id WHERE won.won_product_delete=0";
		$result = $this->ModuleStore->SProductlist($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function getProductListPage($page)
	{
		$sql="SELECT COUNT(*) AS total FROM product WHERE `product_delete`=0";
		$page_info = get_page_info($sql,12,$page);
		$page_url = get_page_url($page,$page_info['page_num']);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}

	/**************************************
	* 查询所有未删除的公共产品信息的分页数据
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function getProductList($start,$pagesize)
	{
		$sql="select * from product where `product_delete`=0 order by `product_id` LIMIT $start,$pagesize";
		$result = $this->ModuleStore->SearchStore($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 按照查询条件来查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function SearchProductListPage($page,$product_is_link,$search_type,$search_key,$store_id)
	{
		if(empty($product_is_link))	
		{
			$sql="SELECT COUNT(*) AS total FROM product WHERE `product_delete`=0";
			if(!empty($search_key))
			{
				if($search_type==0)
				{
					$sql.=" AND `product_sn` like '%".$search_key."%'";
				}
				else 
				{
					$sql.=" AND `product_name` like '%".$search_key."%'";
				}
			}
		
			$page_info = get_page_info($sql,12,$page);
			$page_url = get_page_url($page,$page_info['page_num']);
			return  array('page_info'=>$page_info,'page_url'=>$page_url);
		}
		else 
		{
			$sqla="SELECT `store_product_id` FROM store WHERE `store_product_delete`=0 AND `store_cat_id`=".$store_id;
			$result = $this->ModuleStore->SelectStore($sqla);
			if($result)
			{
				foreach ($result as $rs)
				{
					$resu[]=$rs['store_product_id'];
				}
				$product_idlist=implode(",",$resu);
				if($product_is_link==1)
				{
					$sql="SELECT COUNT(*) AS total  FROM product WHERE `product_delete`=0 AND `product_id` IN (".$product_idlist.")";
					if(!empty($search_key))
					{
						if($search_type==0)
						{
							$sql.=" AND `product_sn` like '%".$search_key."%'";
						}
						else 
						{
							$sql.=" AND `product_name` like '%".$search_key."%'";
						}
					}
				}
				else 
				{
				$sql="SELECT COUNT(*) AS total FROM product WHERE `product_delete`=0 AND `product_id` NOT IN (".$product_idlist.")";
				if(!empty($search_key))
				{
					if($search_type==0)
					{
						$sql.=" AND `product_sn` like '%".$search_key."%'";
					}
					else 
					{
						$sql.=" AND `product_name` like '%".$search_key."%'";
					}
				}
				}
				$page_info = get_page_info($sql,12,$page);
				$page_url = get_page_url($page,$page_info['page_num']);
				return  array('page_info'=>$page_info,'page_url'=>$page_url);
			}
			return null;
		}
	}

	/**************************************
	* 按照查询条件来查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function SearchProductList($start,$pagesize,$product_is_link,$search_type,$search_key,$store_id)
	{
		if(empty($product_is_link))	
		{
			$sql="SELECT * FROM product WHERE `product_delete`=0";
			if(!empty($search_key))
			{
				if($search_type==0)
				{
					$sql.=" AND `product_sn` like '%".$search_key."%'";
				}
				else 
				{
					$sql.=" AND `product_name` like '%".$search_key."%'";
				}
			}
			$sql.=" order by `product_id` LIMIT $start,$pagesize";
			$result = $this ->ModuleStore->StoreList($sql);
			return $result ? $result : NULL;
		}
		else 
		{
			$sqla="SELECT `store_product_id` FROM store WHERE `store_product_delete`=0 AND `store_cat_id`=".$store_id;
			$result = $this->ModuleStore->SelectStore($sqla);
			if($result)
			{
				foreach ($result as $rs)
				{
					$resu[]=$rs['store_product_id'];
				}
				$product_idlist=implode(",",$resu);
				if($product_is_link==1)
				{
					$sql="SELECT * FROM product WHERE `product_delete`=0 AND `product_id` IN (".$product_idlist.")";
					if(!empty($search_key))
					{
						if($search_type==0)
						{
							$sql.=" AND `product_sn` like '%".$search_key."%'";
						}
						else 
						{
						$sql.=" AND `product_name` like '%".$search_key."%'";
						}
					}
				}
				else 
				{
					$sql="SELECT * FROM product WHERE `product_delete`=0 AND `product_id` NOT IN (".$product_idlist.")";
					if(!empty($search_key))
					{
						if($search_type==0)
						{
						$sql.=" AND `product_sn` like '%".$search_key."%'";
						}
						else 
						{
						$sql.=" AND `product_name` like '%".$search_key."%'";
						}
					}
				}
				$sql.=" order by `product_id` LIMIT $start,$pagesize";
				$result = $this ->ModuleStore->StoreList($sql);
				return $result ? $result : NULL;
			}
			return null;
		}
	}

	/**************************************
	* 插入分类信息
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
	**************************************/
	function SaveStore($store)
	{
		$store_cat_name=trim($store['store_cat_name']);
		$store_cat_remark=trim($store['store_cat_remark']);
		$operate_time=date("Y-m-d H:i:s");
		$operater=$_SESSION['AName'];
		$sql="INSERT INTO store_cat (`store_cat_name`,`store_cat_remark`,`operate_time`,`operater`) 
		VALUES ('".$store_cat_name."','".$store_cat_remark."','".$operate_time."','".$operater."')";
		$result = $this->ModuleStore->SaveStore($sql);
		return $result;
	}


	/**************************************
	* 分类信息详情
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
	**************************************/
	function StoreDetail($store_id)
	{
		$sql="SELECT `store_cat_id`,`store_cat_name`,`store_cat_remark`,`store_is_recommend`,`store_delete` FROM store_cat WHERE `store_delete`=0 AND  `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->StoreDetail($sql);
		return $result;
	}

	/**************************************
	* 修改分类信息
	* @access  public
	* @param   array     $store  分类信息组
	*
	* @return  array
	**************************************/
	function UpdateStore($store)
	{
		$store_cat_name=trim($store['store_cat_name']);
		$store_cat_remark=trim($store['store_cat_remark']);
		$store_cat_id=intval($store['store_cat_id']);
		$operate_time=date("Y-m-d H:i:s");
		$operater=$_SESSION['AName'];
		$sql="UPDATE store_cat SET `store_cat_name`='".$store_cat_name."',`operate_time`='".$operate_time."',
		`operater`='".$operater."',`store_cat_remark`='".$store_cat_remark."' WHERE `store_delete`=0 AND `store_cat_id`='".$store_cat_id."'";
		$result = $this->ModuleStore->UpdateStore($sql);
		return $result;
	}

	/**************************************
	* 删除分类信息
	* @access  public
	* @param   int     $store_id  分类信息的ID
	*
	* @return  array
	**************************************/
	function DeleteStore($store_id)
	{
		$sql="UPDATE store_cat SET `store_delete`=1 WHERE `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->UpdateStore($sql);
		return $result;
	}

	/**************************************
	* 根据分类的ID到关联表中查询所关联的产品
	* @access  public
	* @param   int     $store_id  分类信息的ID
	*
	* @return  array
	**************************************/
	function SelectStore($store_id)
	{
		$sql="SELECT `store_product_id`,store_product_sort  FROM store WHERE `store_product_delete`=0 AND `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->SelectStore($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 关联产品和分类
	* @access  public
	* @param   int     $store_id    分类信息的ID
	* @param   int     $product_id  产品的ID
	*
	* @return  array
	**************************************/
	function AddLinkProduct($product_id,$store_id)
	{
		$sql="SELECT COUNT(*) AS total FROM store WHERE `store_cat_id` =".$store_id." AND `store_product_id`=".$product_id;
		$result = $this->ModuleStore->AddLinkProduct($sql);
		if($result)
		{
			$sqla="UPDATE store SET `store_product_delete`=0 WHERE `store_cat_id`=".$store_id." AND `store_product_id`=".$product_id;
		}
		else 
		{
			$store_product_add_time=time();
			$store_product_add_user=$_SESSION['AName'];
			$sqla="INSERT INTO store (`store_cat_id`,`store_product_id`,`store_product_add_time`,`store_product_add_user`) VALUES ('".$store_id."','".$product_id."','".$store_product_add_time."','".$store_product_add_user."')";
		}
		$result = $this->ModuleStore->UpdateStore($sqla);
	}

	/**************************************
	* 解除产品和分类
	* @access  public
	* @param   int     $store_id    分类信息的ID
	* @param   int     $product_id  产品的ID
	*
	* @return  array
	**************************************/
	function DelLinkProduct($product_id,$store_id)
	{
		$sql="UPDATE store SET `store_product_delete`=1 WHERE `store_cat_id`=".$store_id." AND `store_product_id`=".$product_id;
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	/**************************************
	*  按条件搜索分类信息详情
	* @access  public
	* @param   int     $search_type  搜素类别
	* @param   string  $search_key   搜素关键字
	*
	* @return  array
	**************************************/
	function SearchStore($search_type,$search_key)
	{
		if($search_type == 1)
		{
			$sql="SELECT `store_cat_id`,`store_cat_name`,`store_cat_remark`,`store_is_recommend`,`store_delete` FROM store_cat WHERE `store_delete`=0 AND `store_cat_name` LIKE '%".$search_key."%'";
		}
		else 
		{
			$sql="SELECT `store_cat_id`,`store_cat_name`,`store_cat_remark`,`store_is_recommend`,`store_delete` FROM store_cat WHERE `store_delete`=0 AND `store_cat_remark` LIKE '%".$search_key."%'";
		}
		$result = $this->ModuleStore->SearchStore($sql);
		return $result ? $result : NULL;
	}


	/**************************************
	* 在分类的管理产品中推荐产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $rec_value    推荐状态的值
	*
	* @return  array
	**************************************/
	function RecommendStore($store_id,$rec_value)
	{
		if($rec_value==0)
		{
			$sql="UPDATE store_cat SET `store_is_recommend`=1 WHERE `store_delete`=0 AND `store_cat_id`=".$store_id;
		}
		else 
		{
			$sql="UPDATE store_cat SET `store_is_recommend`=0 WHERE `store_delete`=0 AND `store_cat_id`=".$store_id;
		}
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	/**************************************
	* 在分类的管理产品中按条件搜索产品的分页信息
	* @access  public
	* @param   int     $store_id                     分类的ID
	* @param   int     $store_product_is_recommend   推荐状态值
	* @param   int     $store_product_state          发布状态的值
	* @param   int     $store_product_verify         审核状态的值
	* @param   int     $search_type                  关键字状态的值
	* @param   string  $search_key                   关键字的值
	* @param   int     $page                         分页的值
	*
	* @return  array
	**************************************/
	function getManageProductListPage($page,$store_id,$store_product_is_recommend="",$store_product_state="",$store_product_verify="",$search_type="",$search_key="")
	{
		$sqla="SELECT `store_product_id` FROM store WHERE `store_product_delete`=0 AND `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->SelectStore($sqla);
		if($result)
		{
			foreach ($result as $rs)
			{
				$resu[]=$rs['store_product_id'];
			}
			$product_idlist=implode(",",$resu);
			$sql="SELECT count(*) as total FROM product AS pro LEFT JOIN store AS sto ON pro.product_id =sto.store_product_id
			WHERE pro.product_delete=0 AND sto.store_product_delete=0 AND sto.store_cat_id=".$store_id."  AND pro.product_id IN (".$product_idlist.")";
			if(!empty($store_product_is_recommend))
			{
				$store_product_is_recommend = $store_product_is_recommend == 2?0:$store_product_is_recommend ;
				$sql.=" AND sto.store_product_is_recommend = ".$store_product_is_recommend;
			}elseif(!empty($store_product_state))
			{
				$store_product_state = $store_product_state == 2?0:$store_product_state ;
				$sql.=" AND sto.store_product_state = ".$store_product_state;
			}elseif (!empty($search_key))
			{
				if($search_type==0)
				{
				$sql.=" AND pro.product_sn like '%".$search_key."%'";
				}
				else 
				{
				$sql.=" AND pro.product_name like '%".$search_key."%'";
				}
			}elseif (!empty($store_product_verify))
			{
				$store_product_verify=$store_product_verify-1;
				$sql.="AND sto.store_product_verify = ".$store_product_verify;
			}
		
			$page_info = get_page_info($sql,15,$page);
			$page_url = get_page_url($page,$page_info['page_num']);
			return  array('page_info'=>$page_info,'page_url'=>$page_url);
		}
		return null;
	}

	/**************************************
	* 在分类的管理产品中按条件搜索产品
	* @access  public
	* @param   int     $store_id                     分类的ID
	* @param   int     $store_product_is_recommend   推荐状态值
	* @param   int     $store_product_state          发布状态的值
	* @param   int     $store_product_verify         审核状态的值
	* @param   int     $search_type                  关键字状态的值
	* @param   string  $search_key                   关键字的值
	* @param   int     $page                         分页的值
	*
	* @return  array
	**************************************/
	function getManageProductList($start,$pagesize,$store_id,$store_product_is_recommend="",$store_product_state="",$store_product_verify="",$search_type="",$search_key="")
	{
		$sqla="SELECT `store_product_id` FROM store WHERE `store_product_delete`=0 AND `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->SelectStore($sqla);
		if($result)
		{
			foreach ($result as $rs)
			{
				$resu[]=$rs['store_product_id'];
			}
			$product_idlist=implode(",",$resu);
			$sql="SELECT pro.product_id,pro.product_name,pro.product_version,pro.product_price,pro.product_sn,
			sto.store_id,sto.store_cat_id,sto.store_product_id,sto.store_product_add_time,
			sto.store_product_add_user,sto.store_product_state,sto.store_product_is_recommend,
			sto.store_product_verify FROM product AS pro LEFT JOIN store AS sto 
			ON pro.product_id =sto.store_product_id
			WHERE pro.product_delete=0
			AND sto.store_product_delete=0
			AND sto.store_cat_id=".$store_id." 
			AND pro.product_id IN (".$product_idlist.")";
			if(!empty($store_product_is_recommend))
			{
				$store_product_is_recommend = $store_product_is_recommend == 2?0:$store_product_is_recommend ;
				$sql.=" AND sto.store_product_is_recommend = ".$store_product_is_recommend;
			}elseif(!empty($store_product_state))
			{
				$store_product_state = $store_product_state == 2?0:$store_product_state ;
				$sql.=" AND sto.store_product_state = ".$store_product_state;
			}elseif (!empty($search_key))
			{
				if($search_type==0)
				{
					$sql.=" AND pro.product_sn like '%".$search_key."%'";
				}
				else 
				{
					$sql.=" AND pro.product_name like '%".$search_key."%'";
				}
			}elseif (!empty($store_product_verify))
			{
					$store_product_verify=$store_product_verify-1;
					$sql.="AND sto.store_product_verify = ".$store_product_verify;
			}
			$sql.=" order by pro.product_id LIMIT $start,$pagesize";
			$result = $this->ModuleStore->SearchStore($sql);
			return $result ? $result : NULL;
		}
		return null;
	}
	
	/**************************************
	* 在分类的管理产品中发布产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $state_value  发布状态的值
	* @param   int     $product_id   产品的ID
	*
	* @return  array
	**************************************/
	function StateProduct($product_id,$state_value,$store_id)
	{
		$sql="UPDATE store SET `store_product_state`=".$state_value." WHERE `store_cat_id`=".$store_id." AND `store_product_id`=".$product_id;
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	/**************************************
	* 修改store对应产品的排序
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $store_product_sort    要修改的值
	*
	* @return  array
	**************************************/
	function UpdateProductSort($store_id,$product_id,$store_product_sort)
	{
		$sql='UPDATE store SET store_product_sort='.$store_product_sort.' WHERE store_cat_id='.$store_id.' AND store_product_id='.$product_id;
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	/**************************************
	* 在分类的管理产品中推荐产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $rec_value    推荐状态的值
	*
	* @return  array
	**************************************/
	function RecommendProduct($product_id,$rec_value,$store_id)
	{
		$sql="UPDATE store SET `store_product_is_recommend`=".$rec_value." WHERE `store_cat_id`=".$store_id." AND `store_product_id`=".$product_id;
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	/**************************************
	* 在分类的管理产品中审核产品
	* @access  public
	* @param   int     $store_id     分类的ID
	* @param   int     $product_id   产品的ID
	* @param   int     $verify       审核状态的值
	*
	* @return  array
	**************************************/
	function VerifyProduct($product_id,$verify,$store_id)
	{
		$sql="UPDATE store SET `store_product_verify`=".$verify." WHERE `store_cat_id`=".$store_id." AND `store_product_id`=".$product_id;
		$result = $this->ModuleStore->UpdateStore($sql);
	}

	function SelectStorecatname($store_id)
	{
		$sql="SELECT `store_cat_name` FROM store_cat WHERE `store_delete`=0 AND `store_cat_id`=".$store_id;
		$result = $this->ModuleStore->AddLinkProduct($sql);
		return $result;
	}

}